import { ConfigManager } from "../../../com/gengine/resource/ConfigManager";
import { ShopInfo } from "./info/ShopInfo";
import { TShop } from "../../../Message/Db/Tables/TShop";
import { StringHelper } from "../../../com/gengine/utils/StringHelper";
import { Global } from "../../../com/gengine/global/Global";
import { ShopItemInfo } from "./info/ShopItemInfo";
import { TShopSell } from "../../../Message/Db/Tables/TShopSell";
import { DateParser } from "../../common/tools/DateParser";
import { DateUtil } from "../../common/tools/DateUtil";
import { EBind } from "../../../Message/Public/EBind";
import { EPrictUnit } from "../../../Message/Public/EPrictUnit";
type int = number;
//class ShopConfig
    
    export  class ShopConfig
    {
        constructor()
        {
            
            this._map = new Map<any, any> /* flash.utils.Dictionary */();
            this._shopItemInfoMap = new Map<any, any> /* flash.utils.Dictionary */();
            this._tshopArray = [];
            if (ShopConfig._instance != null) 
            {
                throw new Error(" ShopConfig 单例 ");
            }
            this.init();
            return;
        }

        public init(): void
        {
            var loc1=ConfigManager.instance.getJSONByFileName("t_shop.json");
            this.write(loc1);
            loc1 = ConfigManager.instance.getJSONByFileName("t_shop_sell.json");
            this.writeSell(loc1);
            return;
        }

        /* internal  */write(arg1: Object): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=0;
            var loc5=arg1;
            for(loc3 of loc5) 
            {
                loc2 = new ShopInfo();
                loc1 = new TShop();
                loc1.code = loc3.code;
                loc1.name = StringHelper.getString(loc3.name);
                loc1.unit = loc3.unit;
                loc1.isMall = loc3.isMall;
                loc1.buyType = loc3.buyType;
                loc2.tshop = loc1;
                if (Global.isDebugModle) 
                {
                    this._tshopArray.push(loc1);
                    this._map[loc1.code] = loc2;
                    continue;
                }
                if (!(loc1.code <= 5000)) 
                {
                    continue;
                }
                this._tshopArray.push(loc1);
                this._map[loc1.code] = loc2;
            }
            return;
        }

        /* internal  */writeSell(arg1: Object): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=0;
            var loc6=arg1;
            for(loc4 of loc6) 
            {
                if (!Global.isDebugModle) 
                {
                    if (loc4.shopCode > 5000) 
                    {
                        continue;
                    }
                }
                loc3 = new ShopItemInfo();
                loc1 = new TShopSell();
                loc1.shopCode = loc4.shopCode;
                loc1.itemCode = loc4.itemCode;
                loc1.price = loc4.price;
                loc1.offer = loc4.offer;
                loc1.label = loc4.label;
                loc1.canSell = loc4.canSell;
                loc1.activeOffer = loc4.activeOffer;
                loc1.startDt = DateParser.strToDateNormal(loc4.startDt);
                loc1.endDt = DateParser.strToDateNormal(loc4.endDt);
                loc1.canBuyBatch = loc4.canBuyBatch;
                if (!DateUtil.isInDate(loc1.startDt, loc1.endDt)) 
                {
                    continue;
                }
                loc3.tshopSell = loc1;
                loc2 = this.getShopByName(loc1.shopCode);
                loc3.unit = loc2.unit;
                loc2.addShopItemInfo(loc3);
                loc3.isMall = loc2.tshop.isMall == 1;
                this._shopItemInfoMap[loc1.itemCode + "_" + loc1.shopCode] = loc3;
            }
            return;
        }

        public getShopMallArray(arg1: Boolean): Array<any>
        {
            var loc3=null;
            var loc1=[];
            var loc2=0;
            while (loc2 < this._tshopArray.length) 
            {
                if ((loc3 = this._tshopArray[loc2] as TShop) && loc3.isMall == 1) 
                {
                    loc1.push(loc3);
                }
                ++loc2;
            }
            return loc1;
        }

        public getShopByName(arg1: int): ShopInfo
        {
            return this._map[arg1];
        }

        public getShopItemInfoByCode(arg1: int, arg2: int=-1): ShopItemInfo
        {
            var loc1=null;
            var loc2=null;
            if (arg2 == -1) 
            {
                var loc3=0;
                var loc4=this.shopItemInfoMap;
                for (loc2 in loc4) 
                {
                    if (loc2.indexOf(arg1.toString()) == -1) 
                    {
                        continue;
                    }
                    loc1 = this._shopItemInfoMap[loc2];
                    break;
                }
            }
            else 
            {
                loc1 = this._shopItemInfoMap[arg1 + "_" + arg2];
            }
            return loc1;
        }

        public getItemByType(arg1: int, arg2: int, arg3: int=1, arg4: int=-1, arg5: Boolean=true, arg6: int=-1): ShopItemInfo
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            var loc4=this._shopItemInfoMap;
            for(loc1 of loc4) 
            {
                loc2 = loc1.itemInfo.item;
                if (!(arg5 == false || arg5 && loc1.isMall && loc2.bind == EBind._EBindNo)) 
                {
                    continue;
                }
                if (!(loc1.tshopSell.shopCode <= 5001)) 
                {
                    continue;
                }
                if (!(arg6 == -1 || loc1.tshopSell.shopCode == arg6)) 
                {
                    continue;
                }
                if (!(loc2.category == arg1 && loc2.type == arg2)) 
                {
                    continue;
                }
                if (!(arg3 == -1 || loc2.itemLevel == arg3)) 
                {
                    continue;
                }
                if (!(arg4 == -1 || loc2.color == arg4)) 
                {
                    continue;
                }
                return loc1;
            }
            return null;
        }

        public getShopItemInfoArrayByType(arg1: int, arg2: int, arg3: int=-1, arg4: Boolean=true): Array<any>
        {
            var loc2=null;
            var loc3=null;
            var loc1=[];
            var loc4=0;
            var loc5=this._shopItemInfoMap;
            for(loc2 of loc5) 
            {
                if (!(!arg4 || loc2.isMall)) 
                {
                    continue;
                }
                if (!(loc2.tshopSell.shopCode <= 5000)) 
                {
                    continue;
                }
                if (!(arg3 == -1 || loc2.tshopSell.shopCode == arg3)) 
                {
                    continue;
                }
                if (!((loc3 = loc2.itemInfo.item).category == arg1 && loc3.type == arg2)) 
                {
                    continue;
                }
                loc1.push(loc2);
            }
            return loc1;
        }

        public getShopItemInfoArrayByJudge(arg1: Function, arg2: int=0): Array<any>
        {
            var loc2=null;
            var loc3=null;
            var loc1=[];
            var loc4=0;
            var loc5=this._shopItemInfoMap;
            for(loc2 of loc5) 
            {
                loc3 = loc2.itemInfo.item;
                if (!(loc2.tshopSell.shopCode <= 5000)) 
                {
                    continue;
                }
                if (!(arg1(loc3) == true && (arg2 == 0 || loc2.tshopSell.shopCode == arg2))) 
                {
                    continue;
                }
                loc1.push(loc2);
            }
            return loc1;
        }

        public ShopConfig.get shopItemInfoMap(): Map<any, any> /* flash.utils.Dictionary */
        {
            return this._shopItemInfoMap;
        }

        public getShopItemInfo(arg1: int, arg2: int): ShopItemInfo
        {
            var loc1=null;
            var loc3=null;
            var loc4=0;
            var loc2;
            if ((loc2 = this.getShopByName(arg2)) && loc2.items.length > 0) 
            {
                loc4 = 0;
                while (loc4 < loc2.items.length) 
                {
                    if ((loc3 = loc2.items[loc4] as ShopItemInfo).tshopSell.itemCode == arg1) 
                    {
                        loc1 = loc3;
                    }
                    ++loc4;
                }
            }
            return loc1;
        }

        public getGoldItemByType(arg1: int, arg2: int, arg3: int=-1, arg4: int=-1, arg5: int=1701, arg6: int=-1, arg7: int=-1): ShopItemInfo
        {
            var loc1=null;
            var loc2=null;
            if (arg4 == -1) 
            {
                if (!(loc1 = this.getGoldItemByType(arg1, arg2, arg3, EPrictUnit._EPriceUnitGoldBind))) 
                {
                    loc1 = this.getGoldItemByType(arg1, arg2, arg3, EPrictUnit._EPriceUnitGold);
                }
                return loc1;
            }
            var loc3=0;
            var loc4=this._shopItemInfoMap;
            for(loc1 of loc4) 
            {
                if (loc1.tshopSell.shopCode != arg5) 
                {
                    continue;
                }
                if (loc1.unit != arg4) 
                {
                    continue;
                }
                if (!(loc2 = loc1.itemInfo.item)) 
                {
                    continue;
                }
                if (!(loc2.category == arg1 && loc2.type == arg2 && (arg3 == -1 || arg3 == loc2.effect) && (arg6 == -1 || arg6 == loc2.color) && (arg7 == -1 || arg7 == loc2.itemLevel))) 
                {
                    continue;
                }
                return loc1;
            }
            return null;
        }

        public isSkillBookInShop(arg1: int, arg2: int): Boolean
        {
            var loc2=null;
            var loc1=this.getShopByName(arg2);
            if (loc1) 
            {
                var loc3=0;
                var loc4=loc1.items;
                for(loc2 of loc4) 
                {
                    if (loc2.itemInfo.item.effect != arg1) 
                    {
                        continue;
                    }
                    return true;
                }
            }
            return false;
        }

        public isSellInShop(arg1: int): Boolean
        {
            var loc2=null;
            var loc1=false;
            var loc3=0;
            var loc4=this.shopItemInfoMap;
            for (loc2 in loc4) 
            {
                if (loc2.indexOf(arg1.toString()) == -1) 
                {
                    continue;
                }
                loc1 = true;
                break;
            }
            return loc1;
        }

        public static get instance(): ShopConfig
        {
            if (ShopConfig._instance == null) 
            {
                ShopConfig._instance = new ShopConfig();
            }
            return ShopConfig._instance;
        }

        /* internal */ /* var */_map: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_shopItemInfoMap: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_tshopArray: Array<any>;

        /* internal */ static /* var */_instance: ShopConfig;
    }
